<%@ page contentType="text/html;charset=UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="tags" tagdir="/WEB-INF/tags"%>
<%@ taglib prefix="shiro" uri="http://shiro.apache.org/tags"%>
<c:set var="ctx" value="${pageContext.request.contextPath}" />
<html>
<head>
<title>代码表管理</title>
<script type="text/javascript">
	var $grid;
	var $codeValuesTreeGrid;
	var selectTableRow;//选中的行数据
	$(function() {
		$grid = $("#grid").datagrid(
				$.extend(datagridOption, {
					url : "${ctx}/basic/codeTable/dataGrid",
					columns : [ [
							{
								field : 'ck',
								checkbox : true
							},
							{
								field : 'tableName',
								title : '表名称',
								width : 120,
								sortable : true
							},
							{
								field : 'tableCode',
								title : '标识码',
								width : 120
							},
							{
								field : 'remark',
								title : '备注',
								width : 200,
								sortable : true
							},
							{
								field : 'enable',
								title : '是否启用',
								width : 100,
								formatter : function(value, row, index) {
									return '<input class="easyui-switchbutton "' + (value == 1 ? "checked" : "")
											+ ' data-options="readonly:true,onText:\'是\',offText:\'否\'">';
								}
							} ] ],
					onClickRow : function(rowIndex, rowData) {
						selectTableRow = rowData;
						$("#cc").layout("panel", "east").panel({
							title : "代码表【" + rowData.tableName + "】明细管理"
						});

						$codeValuesTreeGrid.treegrid("clearSelections");//清除所有选中的行，解决切换不同代码表后还保留代码值中上次选中行bug
						refreshCodeValues(rowData);//刷新代码表明细
					}
				}));
		$grid.datagrid('enableFilter', [ {
			field : 'enable',
			type : 'combobox',
			javaType : 'I',
			options : {
				panelHeight : 'auto',
				data : [ {
					value : '',
					text : '全部'
				}, {
					value : '1',
					text : '是'
				}, {
					value : '2',
					text : '否'
				} ],
				onChange : function(value) {
					if (value == '') {
						$grid.datagrid('removeFilterRule', 'enable');
					} else {
						$grid.datagrid('addFilterRule', {
							field : 'enable',
							op : 'EQ',
							value : value
						});
					}
					$grid.datagrid('doFilter');
				}
			}
		} ]);
		$("#enableSB").switchbutton({
			onText : "是",
			offText : "否",
			checked : true,
			onChange : function(checked) {
				if (checked) {
					$("#enable").val(1);
				} else {
					$("#enable").val(2);
				}
			}
		});

		$codeValuesTreeGrid = $("#codeValuesTreeGrid").treegrid(
				$.extend(datagridOption, {
					method : "get",
					url : "",
					idField : 'id',
					treeField : 'codeName',
					parentField : "pid",
					remoteSort : true,
					multiSort : true,
					rownumbers : true,
					singleSelect : true,
					striped : true,
					selectOnCheck : false,
					checkOnSelect : false,
					fit : true,
					cascadeCheck : true,//级联勾选
					dataPlain : true, //该属性用以启用当前 easyui-treegrid 控件对平滑数据格式的支持
					toolbar : '#codett',
					columns : [ [
							{
								field : 'ck',
								checkbox : true
							},
							{
								field : 'codeTable',
								title : '所属代码表',
								sortable : true,
								formatter : function(value, row, index) {
									return value ? value.tableName : "";
								}
							},
							{
								field : 'codeName',
								title : '代码名称',
								sortable : true
							},
							{
								field : 'parentCodeName',
								title : '父代码名称'
							},
							{
								field : 'codeValue',
								title : '代码值',
								sortable : true
							},
							{
								field : 'remark',
								title : '备注',
								sortable : true
							},
							{
								field : 'enable',
								title : '是否启用',
								formatter : function(value, row, index) {
									return '<input class="easyui-switchbutton "' + (value == 1 ? "checked" : "")
											+ ' data-options="readonly:true,onText:\'是\',offText:\'否\'">';
								}
							}, {
								field : 'iconCls',
								title : '图标Class',
								hidden : true
							} ] ],
					onClickRow : function(rowIndex, rowData) {

					},
					onLoadSuccess : function() {
						$("#codeValuesTreeGrid").parent().find(".datagrid-btable .easyui-switchbutton").switchbutton();
					},//异步加载完毕时手动绘制switchbutton
					enableHeaderClickMenu : true, //此属性开启表头列名称右侧那个箭头形状的鼠标左键点击菜单
					enableHeaderContextMenu : true, //此属性开启表头列名称右键点击菜单
					enableRowContextMenu : false
				}));

		$("#codeEnableSB").switchbutton({
			onText : "是",
			offText : "否",
			checked : true,
			onChange : function(checked) {
				if (checked) {
					$("#codeEnable").val(1);
				} else {
					$("#codeEnable").val(2);
				}
			}
		});

		$("#add").click(function() {
			$("#fm").form("clear");
			$('#fm').form('load', {
				enable : 1
			});
			$("#enableSB").switchbutton('check');
			//$("#radio1").click();

			$("#dlg").dialog("open").dialog("setTitle", "新增代码表");
		});
		$("#codeadd").click(function() {
			fn_initCombo();
			if (!selectTableRow) {
				$.messager.show({
					icon : "error",
					msg : "请先选中代码表中的行"
				});
				return false;
			}
			$("#codefm").form("clear");
			var codeSelected = $codeValuesTreeGrid.datagrid("getSelected");
			$('#codefm').form('load', {
				enable : 1,
				codeTable : selectTableRow.id,
				codeParentId : !codeSelected ? '' : codeSelected.id
			});
			$("#codeEnableSB").switchbutton('check');
			//$("#radio3").click();

			$("#codedlg").dialog("open").dialog("setTitle", "新增代码");
		});
		$("#edit").click(function() {
			var rowData = $grid.datagrid("getSelected");
			if (rowData == null) {
				$.messager.show({
					msg : "您未选中行，无法编辑",
					icon : "warning",
					position : "bottomRight"
				});
			} else {
				if (rowData.editable != 1) {
					$.messager.show({
						msg : "该代码表不可编辑",
						icon : "warning",
						position : "bottomRight"
					});
				} else {
					$("#dlg").dialog("open").dialog("setTitle", "编辑代码表");
					$("#fm").form("load", rowData);
					$("#enableSB").switchbutton(rowData.enable == 1 ? "check" : "uncheck");
				}
			}
		});
		$("#codeedit").click(function() {
			if (!selectTableRow) {
				showWarnInfo("请先选中代码表中的行");
				return false;
			}
			if (selectTableRow.editable != 1) {
				showWarnInfo("该代码表不可编辑");
				return false;
			}
			fn_initCombo();
			var rowData = $codeValuesTreeGrid.datagrid("getSelected");
			if (rowData == null) {
				$.messager.show({
					msg : "您未选中行，无法编辑",
					icon : "warning",
					position : "bottomRight"
				});
			} else {
				$("#codefm").form("load", $.extend(rowData, {
					codeTable : selectTableRow.id,
				}));
				$("#codeEnableSB").switchbutton(rowData.enable == 1 ? "check" : "uncheck");
				$("#codedlg").dialog("open").dialog("setTitle", "编辑代码");
				$("#iconCls").comboicons("setValue", rowData.iconCls);
			}
		});
		$("#delete").click(function() {
			/* multiDeleteDataGrid($grid,"${ctx}/basic/codeTable/delete");
			$codeValuesTreeGrid.treegrid("load"); */
			var rowData = $grid.datagrid("getChecked");
			if (rowData == null) {
				$.messager.show({
					msg : "您未选中行，无法删除",
					icon : "warning",
					position : "bottomRight"
				});
			} else {
				var ids = "";
				$.messager.confirm("您确定要进行该操作？", function(c) {
					if (c) {
						for (var i = 0; i < rowData.length; i++) {
							ids += rowData[i].id + ",";
						}
						ids = ids.substring(0, ids.length - 1);
						$.post("${ctx}/basic/codeTable/delete", {
							ids : ids
						}, function(result) {
							$.messager.show({
								msg : result.msg,
								icon : "info",
								position : "bottomRight"
							});
							if (result.success) {
								$grid.datagrid("uncheckAll");//zst添加，清除上次删除选中的行，解决连续删除时id不存在的bug
								$grid.datagrid("load");
								$codeValuesTreeGrid.treegrid("load");
							}
						}, 'json');
					}
				});
			}
		});
		$("#codedelete").click(function() {
			multiDeleteTreeGrid($codeValuesTreeGrid, "${ctx}/basic/codeValues/delete");
		});

		$("#codesearch").click(function() {
			$codeValuesTreeGrid.treegrid("clearSelections");//清除所有选中的行，解决始终保留代码值中上次选中行bug

			$codeValuesTreeGrid.treegrid('load', {
				search_LIKE_codeName : $("#search_LIKE_codeName").val()
			});
		});

		$("#codeall").click(function() {
			$codeValuesTreeGrid.treegrid("clearSelections");//清除所有选中的行，解决始终保留代码值中上次选中行bug
			$codeValuesTreeGrid.treegrid({
				url : "${ctx}/basic/codeValues/dataGrid"
			});
		});

		$("#excelImport").click(function() {
			$.easyui.importor.showImportor({
				importNo : "CodeTable",
				onExportSuccess : function(file, data) {
					$grid.datagrid('load');
				}
			});
		});
		$("#excelImportValues").click(function() {
			$.easyui.importor.showImportor({
				importNo : "CodeValues",
				onExportSuccess : function(file, data) {
					$codeValuesTreeGrid.treegrid({
						url : "${ctx}/basic/codeValues/dataGrid?search_EQ_codeTable.id=" + selectTableRow.id
					});
				}
			});
		});

		$("#excelExport").click(function() {
			var url = "${ctx}/basic/codeTable/excelExport?" + $.param($grid.datagrid('options').queryParams);
			if ($grid.datagrid("options").filterRules) {
				url += "&filterRules=" + JSON.stringify($grid.datagrid("options").filterRules);
			}
			window.parent.location.href = url;
		});
		$("#excelExportValues").click(
				function() {
					if (selectTableRow != null) {
						var url = "${ctx}/basic/codeTable/excelExportValues?search_EQ_codeTable.id=" + selectTableRow.id
								+ "&search_LIKE_codeName=" + $("#search_LIKE_codeName").val();
					} else {
						var search_LIKE_codeName = $("#search_LIKE_codeName").val();
						if (search_LIKE_codeName != null) {
							var url = "${ctx}/basic/codeTable/excelExportValues?search_LIKE_codeName=" + $("#search_LIKE_codeName").val();
						} else {
							var url = "${ctx}/basic/codeTable/excelExportValues?" + $.param($grid.datagrid('options').queryParams);
						}
					}
					if ($grid.datagrid("options").filterRules) {
						url += "&filterRules=" + JSON.stringify($grid.datagrid("options").filterRules);
					}
					window.parent.location.href = url;
				});
	});
	function fn_initCombo() {
		$('#codeParentId').combotree({
			url : '${ctx}/basic/codeValues/comboTree?search_EQ_codeTable.id=' + selectTableRow.id,
			method : 'get',
			editable : false,
			idField : 'id',
			textField : 'text',
			parentField : 'pid',
			dataPlain : true
		});
	}
	function fn_save() {
		$("#fm").form("submit", {
			url : "${ctx}/basic/codeTable/update",
			success : function(data) {
				$grid.datagrid("load");
				$.messager.show({
					msg : "保存成功",
					icon : "info",
					position : "bottomRight"
				});
				$("#dlg").dialog('close');
			}
		});
	}
	function fn_codesave() {
		$("#codefm").form("submit", {
			url : "${ctx}/basic/codeValues/update",
			success : function(data) {
				$codeValuesTreeGrid.treegrid("clearSelections");//清除所有选中的行，解决始终保留代码值中上次选中行bug
				$codeValuesTreeGrid.treegrid({
					url : "${ctx}/basic/codeValues/dataGrid?search_EQ_codeTable.id=" + selectTableRow.id
				});
				showInfoForCallBack(data);
				$("#codedlg").dialog('close');
			}
		});
	}

	//刷新values列表
	function refreshCodeValues(rowData) {
		if (!rowData) {
			showAlert("请先选择中类信息");
			return false;
		} else {
			$codeValuesTreeGrid.treegrid({
				url : "${ctx}/basic/codeValues/dataGrid?search_EQ_codeTable.id=" + rowData.id
			});
			$codeValuesTreeGrid.treegrid('reload', {
				search_LIKE_codeName : ''
			});
		}

	}

	function search() {
		/*$codeValuesTreeGrid.datagrid('enableFilter',[{
			field : 'codeTable',
			searchField :'codeTable.tableName'
		},{
			field : 'enable',
			type : 'combobox',
			javaType : 'I',
			options : {
				panelHeight : 'auto',
				data : [ {
					value : '',
					text : '全部'
				}, {
					value : '1',
					text : '是'
				}, {
					value : '2',
					text : '否'
				} ],
				onChange : function(value) {
					if (value == '') {
						$codeValuesTreeGrid.treegrid('removeFilterRule', 'docStatus');
					} else {
						$codeValuesTreeGrid.treegrid('addFilterRule', {
							field : 'enable',
							op : 'EQ',
							value : value
						});
					}
					$codeValuesTreeGrid.datagrid('doFilter');
				}
			}
		} ]); */
	}
</script>
</head>
<body>
	<div data-options="region:'center'" data-options="fit:true,border:false" style="padding: 1px;">
		<div id="cc" class="easyui-layout" data-options="fit:true,border:false" style="width: 550px; height: 440px;">
			<div data-options="region:'center',border:false" style="padding: 1px; witdh: 700px; ">
				<table id="grid">
				</table>
				<div id="tt" class="row">
					<div class="col-md-12">
						<!-- <span>代码表名称：</span>
					<input type="text" id="search_LIKE_tableName" style="margin-left: 2px; margin-right: 2px; padding-left: 2px; padding-right: 3px;" />
					<a id="search" class="easyui-linkbutton easyui-tooltip" data-options="plain: true, iconCls: 'icon-hamburg-zoom',content:'查询'"></a> -->
						<shiro:hasPermission name="codeTable:update">
							<button id="add" type="button" class="btn btn-primary">
								<span class="glyphicon glyphicon-plus"></span> 新增
							</button>
							<button id="edit" type="button" class="btn btn-primary">
								<span class="glyphicon glyphicon-pencil"></span> 编辑
							</button>
						</shiro:hasPermission>
						<shiro:hasPermission name="codeTable:delete">
							<button id="delete" type="button" class="btn btn-danger">
								<span class="glyphicon glyphicon-minus"></span> 删除
							</button>
						</shiro:hasPermission>
						<shiro:hasPermission name="codeTable:import">
							<button id="excelImport" type="button" class="btn btn-primary">
								<span class="glyphicon glyphicon-open"></span> Excel导入
							</button>
						</shiro:hasPermission>
						<shiro:hasPermission name="codeTable:export">
							<button id="excelExport" type="button" class="btn btn-primary">
								<span class="glyphicon glyphicon-save"></span> Excel导出
							</button>
						</shiro:hasPermission>
					</div>
				</div>
				<shiro:hasPermission name="codeTable:update">
					<div id="dlg" class="easyui-dialog" style="width: 360px; height: 315px; padding: 10px 20px"
						data-options="resizable:true,modal:true, buttons:'#dlg-btn',closed:true">
						<form id="fm" method="post">
							<input type="hidden" name="id" />
							<table id="tbl">
								<tr>
									<td><label>代码表名称:</label></td>
									<td><input id="tableName" name="tableName" class="easyui-validatebox" data-options="required:true" /></td>
								</tr>
								<tr>
									<td><label>标识码:</label></td>
									<td><input name="tableCode" class="easyui-validatebox" data-options="required:true" /></td>
								</tr>
								<tr>
									<td><label>是否启用:</label></td>
									<td><input type="hidden" id="enable" name="enable" /> <input id="enableSB" /></td>
								</tr>
								<tr>
									<td><label>备注:</label></td>
									<td><input name="remark" class="easyui-textbox" data-options="multiline:true,width:185,height:60"></td>
								</tr>
							</table>
						</form>
						<div id="dlg-btn">
							<button id="add" type="button" class="btn btn-primary" onclick="fn_save();">
								<span class="glyphicon glyphicon-ok"></span> 保存
							</button>
							<button id="edit" type="button" class="btn btn-primary" onclick="$('#dlg').dialog('close');">
								<span class="glyphicon glyphicon-remove"></span> 取消
							</button>
						</div>
					</div>
				</shiro:hasPermission>
			</div>
			<div data-options="region:'east', title:'代码值', split: true, collapsible:true,border:false"
				style="padding: 1px; witdh: 700px; ">
				<table id="codeValuesTreeGrid">
				</table>
				<div id="codett" class="row">
					<div class="col-md-6">
						<shiro:hasPermission name="codeTable:list">
							<form class="form-grid-search form-inline">
								<div class="form-group">
									<input id="search_LIKE_codeName" type="text" class="form-control" name="search_LIKE_codeName" placeholder="代码名称" />
								</div>
								<button type="button" id="codesearch" class="btn btn-primary">
									<span class="glyphicon glyphicon-search"></span>
								</button>
							</form>
						</shiro:hasPermission>
					</div>
					<div class="col-md-6">
						<shiro:hasPermission name="codeTable:update">
							<button id="codeadd" type="button" class="btn btn-primary">
								<span class="glyphicon glyphicon-plus"></span> 新增
							</button>
							<button id="codeedit" type="button" class="btn btn-primary">
								<span class="glyphicon glyphicon-pencil"></span> 编辑
							</button>
						</shiro:hasPermission>
						<div class="btn-group">
							<button type="button" class="btn btn-sm btn-primary">选项</button>
							<button type="button" class="btn btn-primary btn-sm dropdown-toggle" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
								<span class="caret"></span> <span class="sr-only">Toggle Dropdown</span>
							</button>
							<ul class="dropdown-menu dropdown-menu-right">
								<li><shiro:hasPermission name="codeTable:delete">
										<a id="codedelete">
											<span class="glyphicon glyphicon-minus"></span> 删&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;除
										</a>
									</shiro:hasPermission></li>
								<li role="separator" class="divider"></li>
								<li><shiro:hasPermission name="codeTable:import">
										<a id="excelImportValues">
											<span class="glyphicon glyphicon-open"></span> Excel导入
										</a>
									</shiro:hasPermission></li>
								<li><shiro:hasPermission name="codeTable:export">
										<a id="excelExportValues">
											<span class="glyphicon glyphicon-save"></span> Excel导出
										</a>
									</shiro:hasPermission></li>
								<li role="separator" class="divider"></li>
								<li><a id="codeall">
										<span class="glyphicon glyphicon-search"></span> 显示全部
									</a></li>
							</ul>
						</div>
					</div>
				</div>
				<shiro:hasPermission name="codeTable:update">
					<div id="codedlg" class="easyui-dialog" style="width: 370px; height: 385px; padding: 10px 20px"
						data-options="resizable:true,modal:true, buttons:'#codedlg-btn',closed:true">
						<form id="codefm" method="post">
							<input type="hidden" name="id" />
							<input type="hidden" name="codeTable" />
							<table>
								<tr>
									<td><label>代码名称:</label></td>
									<td><input name="codeName" class="easyui-validatebox" data-options="required:true"></td>
								</tr>
								<tr>
									<td><label>父代码名称:</label></td>
									<td><input id="codeParentId" name="codeParentId" class="easyui-combotree easyui-validatebox"></td>
								</tr>
								<tr>
									<td><label>代码值:</label></td>
									<td><input name="codeValue" class="easyui-validatebox" data-options="required:true"></td>
								</tr>
								<tr>
									<td><label>图标:</label></td>
									<td><input id="iconCls" name="iconCls" class="easyui-comboicons"
											data-options="width: 180, autoShowPanel: false, multiple: false, size: '16'"></td>
								</tr>
								<tr>
									<td><label>是否启用:</label></td>
									<td><input type="hidden" id="codeEnable" name="enable"> <input id="codeEnableSB"></td>
								</tr>
								<tr>
									<td><label>备注:</label></td>
									<td><input name="remark" class="easyui-textbox" data-options="multiline:true,width:185,height:60"></td>
								</tr>
							</table>
						</form>
						<div id="codedlg-btn">
							<button id="add" type="button" class="btn btn-primary" onclick="fn_codesave(); ">
								<span class="glyphicon glyphicon-ok"></span> 保存
							</button>
							<button id="edit" type="button" class="btn btn-primary" onclick="$('#codedlg').dialog('close');">
								<span class="glyphicon glyphicon-remove"></span> 取消
							</button>
						</div>
					</div>
				</shiro:hasPermission>
			</div>
		</div>
	</div>
</body>
</html>
